import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */
public class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        int i = 0;
        for(i = 0; i < s.length();i++){
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{'|| ch == '['){
                stack.push(ch);
            }else{
                if(stack.empty()){
                    return false;
                }
                if(ch == ')' || ch == '}' || ch == ']'){
                    if(stack.peek() == '(' && ch == ')'){
                        stack.pop();
                    }else if(stack.peek() == '{' && ch == '}'){
                        stack.pop();
                    }else if(stack.peek() == '[' && ch == ']'){
                        stack.pop();
                    }
                    else{
                        return false;
                    }
                }
            }
        }
        if(stack.empty() && i == s.length()){
            return true;
        }else{
            return false;
        }
    }
}
